/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include <stdlib.h>
#include "Binder.h"
#include "../shared/StringUtil.h"
#include "../Kernel.h"

extern Kernel* g_kernel;

Binder::Binder(void)
{
}


/** Bind a key to an action */
void Binder::Bind( int key, const std::string &function)
{
    m_bindings[key] = function;
}
/** Unbind a key from an action */
void Binder::UnBind( int key )
{
    std::map<int, std::string >::iterator it = m_bindings.begin();
    for(; it != m_bindings.end(); it++ )
    {
        if ( it->first == key ) {
            m_bindings.erase( it );
            return;
        }
    }
}
/** Unbind all keys */
void Binder::UnBindAll()
{
    m_bindings.clear();
}
/** Execute the key bind, if any */
void Binder::Exec( int key )
{
    // first test to see if it is a valid bind
    if ( m_bindings.find( key ) != m_bindings.end() )
    {
        // exec the string
        g_kernel->GetConsole()->Exec( m_bindings[key] );
    }
}

Binder::~Binder(void)
{
    //UnBindAll();
}


